const logoinIdValidator = new FieldValidator("txtLoginId", async function (
  val
) {
  if (!val) {
    return "请填写账号";
  }
  const resp = await API.exists(val);
  if (resp.data) {
    return "该账号已被占用，请重新选择一个账号名";
  }
});

const nicknameValidator = new FieldValidator("txtNickname", function (val) {
  if (!val) {
    return "请填写昵称";
  }
});

const loginPwdValidator = new FieldValidator("txtLoginPwd", function (val) {
  if (!val) {
    return "请填写密码";
  }
});

const loginPwdConfirmValidator = new FieldValidator(
  "txtLoginPwdConfirm",
  function (val) {
    if (!val) {
      return "请填写确认密码";
    }
    if (val !== loginPwdValidator.input.value) {
      return "两次密码不一致";
    }
  }
);

const form = $(".user-form");
form.onsubmit = async function (e) {
  e.preventDefault();
  const ret = await FieldValidator.validate(
    logoinIdValidator,
    nicknameValidator,
    loginPwdValidator,
    loginPwdConfirmValidator
  );

  if (!ret) {
    return;
  }

  // 1.第一种获取表单的值
  /* const obj = {
    loginId: loginPwdValidator.input.value,
    loginPwd: loginPwdValidator.input.value,
    nickname: nicknameValidator.input.value,
  }; */

  // 第二种形式
  const formData = new FormData(form);
  const resp = await API.reg(Object.fromEntries(formData.entries()));
  if (resp.code === 0) {
    alert("注册成功");
    location.href = "./login.html";
  }
};
